import * as moment from 'moment';

//只显示后六位字符
function hideStr (value:string, replaceStr:string = '') {
    if(!value) return ''
    let bankNumArr = value.split(''),
    bankNumArr1 = bankNumArr.slice(-6, bankNumArr.length); //后六位数字
    bankNumArr = [replaceStr,...bankNumArr1]
    return bankNumArr.join('');
};

function formatter(row: any, column: any) {
    return moment(row[column.property]).format('YYYY-MM-DD');
}


/**
 * @Author: HG
 * @Date: 2019-01-03 16:51:21
 * @Desc: 日期工具
 * @return  {Object}
 */
 function DateTools() {
    let date = new Date(),
        year = date.getFullYear(),
        month = date.getMonth() + 1;

    return {
        /**
         * 月初
         */
        earlyMonth: function() {
            return `${year}-` + (month > 9 ? month : `0${month}`) + '-01';
        },

        /**
         * 月末
         */
        endMonth: function() {
            return `${year}-` + (month > 9 ? month : `0${month}`) + '-' + moment(date).daysInMonth().toString();
        },

        /**
         * 当前年份
         */
        getYear: function() {
            return date.getFullYear();
        },

        /**
         * 当前月份
         */
        getCurrentMonth: function() {
            return month > 9 ? month : `0${month}`;
        }
    }
}
//只显示前 n 位文字, 默认8位, 省略部分用传入的字符代替
function ellipsis (value:string, replaceStr:string = '', n:number = 8){
    if(!value) return ''
    if(value.length <= n) replaceStr = ''
    let beforeStr = value.split('')
    beforeStr = beforeStr.slice(0, n)
    beforeStr = [...beforeStr, replaceStr]
    return beforeStr.join('')
}
function dateAdd(interval:string, number:number, date) {
    switch (interval) {
        case "y": {
            date.setFullYear(date.getFullYear() + number);
            return date;
            break;
        }
        case "q": {
            date.setMonth(date.getMonth() + number * 3);
            return date;
            break;
        }
        case "m": {
            date.setMonth(date.getMonth() + number);
            return date;
            break;
        }
        case "w": {
            date.setDate(date.getDate() + number * 7);
            return date;
            break;
        }
        case "d": {
            date.setDate(date.getDate() + number);
            return date;
            break;
        }
        case "h": {
            date.setHours(date.getHours() + number);
            return date;
            break;
        }
        case "m": {
            date.setMinutes(date.getMinutes() + number);
            return date;
            break;
        }
        case "s": {
            date.setSeconds(date.getSeconds() + number);
            return date;
            break;
        }
        default: {
            date.setDate(d.getDate() + number);
            return date;
            break;
        }
    }
}
// 获取涂抹图片路径
function getEndorsementFileName(imgUrl: string) {
    var filename = imgUrl.replace(/^.+\.com\//, '').replace(/\?.+/, '');
    var suffix = filename.substr(filename.lastIndexOf('.'), filename.length);
    return filename.replace(suffix, `-1${suffix}`)
}
function getEndorsementFileName1(imgUrl: string) {
    var suffix = imgUrl.substr(imgUrl.lastIndexOf('.'), imgUrl.length);
    return imgUrl.replace(suffix, `-1${suffix}`)
}
function memberStatusName(val: string|number) {
    let statusName = ''
    switch (val) {
        case 11:
            statusName = '业务授权未成功的联系人';
            break;
        case 12:
            statusName = '业务授已成功的联系人';
            break;
        case 21:
            statusName = '企业审核失败';
            break;
        case 22:
            statusName = '企业待审核';
            break;
        case 23:
            statusName = '企业审核成功';
            break;
        case 31:
            statusName = '业务授权审核失败';
            break;
        case 32:
            statusName = '业务授权待审核';
            break;
        case 33:
            statusName = '业务授权审核成功';
            break;
        case 41:
            statusName = '小额鉴权失败';
            break;
        case 42:
            statusName = '待小额鉴权';
            break;
        case 43:
            statusName = '小额鉴权成功';
            break;
        case 44:
            statusName = '账户待开户';
            break;
    }
    return statusName
}
// 机构类型Types
function orgNatureNameFun(val: string) {
    let orgNatureName = ''
    switch (val) {
        case '0101':
            orgNatureName = '法人企业';
            break;
        case '0102':
            orgNatureName = '非法人企业';
            break;
        case '0103':
            orgNatureName = '事业单位';
            break;
        case '0104':
            orgNatureName = '社会团体';
            break;
        case '0105':
            orgNatureName = '党政机关';
            break;
    }
    return orgNatureName
}
// 企业所有制类型
function orgOwnerShipTypeNameFun(val: string) {
    let orgOwnerShipTypeName = ''
    switch (val) {
        case '110':
            orgOwnerShipTypeName = '国有控股企业';
            break;
        case '120':
            orgOwnerShipTypeName = '集体控股企业';
            break;
        case '170':
            orgOwnerShipTypeName = '私人控股企业';
            break;
        case '200':
            orgOwnerShipTypeName = '港澳台商控股企业';
            break;
        case '300':
            orgOwnerShipTypeName = '外商控股企业';
            break;
    }
    return orgOwnerShipTypeName
}
// 是否长期有效
function certDeadlineNameFun(val: string|number) {
    let certDeadlineName = ''
    switch (val) {
        case 1:
            certDeadlineName = '是';
            break;
        case 0:
            certDeadlineName = '否';
            break;
    }
    return certDeadlineName
}
// 高管职位 
function manageRelationShipNameFun(val: string) {
    let manageRelationShipName = ''
    switch (val) {
        case '0100':
            manageRelationShipName = '法人代表';
            break;
        case '0101':
            manageRelationShipName = '公司董事长';
            break;
        case '0102':
            manageRelationShipName = '公司总经理/厂长/CEO';
            break;
        case '0103':
            manageRelationShipName = '财务主管/CFO';
            break;
        case '0104':
            manageRelationShipName = '授权经办人';
            break;
    }
    return manageRelationShipName
}
// 证件类型
function certTypeNameFun(val: string) {
    let certTypeName = ''
    switch (val) {
        case 'Ind01':
            certTypeName = '身份证'
            break;
    }
    return certTypeName
}
// 出资方式
function relationShipNameFun(val: string) {
    let relationShipName = ''
    switch (val) {
        case '5201':
            relationShipName = '资金(资本构成)';
            break;
        case '5202':
            relationShipName = '技术(资本构成)';
            break;
        case '5203':
            relationShipName = '实物(资本构成)';
            break;
        case '5204':
            relationShipName = '权利(资本构成)';
            break;
        case '5205':
            relationShipName = '其他(资本构成)';
            break;
    }
    return relationShipName
}
function checkPhoneRex(phoneCustomer: string) {
    let phoneCustomerVal: string = phoneCustomer;
    if (phoneCustomerVal.length == 11) {
        if (!/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(phoneCustomerVal)) {
            return false;
        }else {
            return true;
        }
    }else {
        return false;
    }
}

function checkPhoneRexInviter(phoneCustomer: string) {
    let phoneCustomerVal: string = phoneCustomer;
    if (phoneCustomerVal == null || phoneCustomerVal == '') {
        return true;
    } else {
        if (phoneCustomerVal.length == 11) {
            if (!/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(phoneCustomerVal)) {
                return false;
            }else {
                return true;
            }
        } else {
            return false;
        }
    }
}

export {
    hideStr,
    formatter,
    DateTools,
    ellipsis,
    dateAdd,
    memberStatusName,
    orgNatureNameFun,
    orgOwnerShipTypeNameFun,
    certDeadlineNameFun,
    manageRelationShipNameFun,
    certTypeNameFun,
    relationShipNameFun,
    getEndorsementFileName,
    getEndorsementFileName1,
    checkPhoneRex,
    checkPhoneRexInviter
}